$(document).ready(function(){
	//1.点击按钮可以在屏幕中间显示一个窗口

	var centerwin = $("#center").mywin({left:"center", top:"center"});
	var leftwin    = $("#left").mywin({left:"left", top:"bottom"},function(){
		leftwin.slideUp("slow");
	});

	$("#centerpop").click(function(){
		centerwin.show();
	});

	$("#leftpop").click(function(){
		leftwin.slideDown("slow");
	});
});

/**
 *@param position
 */

$.fn.mywin = function(position,hidefunc){
	if(position  && position instanceof Object){
		var positionleft = position.left;
		var positiontop  = position.top;
		var windowobj = $(window);
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		var scrollLeft  = $(window).scrollLeft();
		var scrollTop  = $(window).scrollTop();
		var cwinwidth = this.outerWidth(true);
		var cwinheight = this.outerHeight(true);
		var left;
		var top;
		var currentwin = this;
		function callLeft(positionleft,scrollLeft,browserwidth,cwinwidth){
			if(position  && typeof positionleft == "string"){
				if(positionleft == "center"){
					left = scrollLeft + (browserwidth-cwinwidth)/2;
				}
				else if(positionleft == "left" ){
					left = scrollLeft;
				}
				else if(positionleft == "right"){
					left = scrollLeft + browserwidth - cwinwidth;
				}
				else{
					left = scrollLeft + (browserwidth-cwinwidth)/2;
				}

			}
			else if(position  && typeof positionleft == "number"){
					left = positionleft;
			}
			else{
				left = 0;
			}
			currentwin.data("positionleft",positionleft);
		}
		callLeft(positionleft,scrollLeft,browserwidth,cwinwidth);


		function callTop(positiontop,scrollTop,browserheight,cwinheight){
			if(position  && typeof positiontop == "string"){
				if(positiontop == "center"){
					top  = scrollTop + (browserheight-cwinheight)/2;
				}
				else if(positiontop == "top" ){
					top = scrollTop;
				}
				else if(positiontop == "bottom"){
					top = scrollTop + browserheight - cwinheight;
				}
				else{
					top  = scrollTop + (browserheight-cwinheight)/2;
				}

			}
			else if(position  && typeof positiontop == "number"){
					top = positiontop;
			}
			else{
				top = 0;
			}
			currentwin.data("positiontop",positiontop);
		}

		callTop(positiontop,scrollTop,browserheight,cwinheight);

		$(window).scroll(function(){
			var browserwidth = $(window).width();
			var browserheight = $(window).height();
			var scrollLeft  = $(window).scrollLeft();
			var scrollTop  = $(window).scrollTop();
			callLeft(currentwin.data("positionleft"),scrollLeft,browserwidth,cwinwidth);
			callTop(currentwin.data("positiontop"),scrollTop,browserheight,cwinheight);
			currentwin.css("left", left).css("top", top);
		});

		$(window).resize(function(){
			var browserwidth = $(window).width();
			var browserheight = $(window).height();
			var scrollLeft  = $(window).scrollLeft();
			var scrollTop  = $(window).scrollTop();
			callLeft(currentwin.data("positionleft"),scrollLeft,browserwidth,cwinwidth);
			callTop(currentwin.data("positiontop"),scrollTop,browserheight,cwinheight);
			currentwin.css("left", left).css("top", top);
		});

		currentwin.css("left", left).css("top", top);

		currentwin.children(".title").children("img").click(function(){
			if(!hidefunc){
				currentwin.hide();
			}
			else{
				hidefunc();
			}

		});
	}
	return currentwin;
}